<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="shiro," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="一些废话在我写这篇博文时我大概断断续续学了三四天shiro,期间因为看的不认真的缘故 ，我对shiro一知半解根本算不上系统的学习。在我学习一样东西是我奉行的策略就是边学边做，于是我花了半天的时间模仿着别人写了一个例子，终于对shiro的权限控制流程有了个大致的了解。对于shiro张开涛老师讲的很细致.下面就开始我们的讲解。 shiro简介 Apache Shiroa是一个强大且易用的Java安全">
<meta name="keywords" content="shiro">
<meta property="og:type" content="article">
<meta property="og:title" content="shiro学习笔记[1]">
<meta property="og:url" content="http://www.flyfood.name/2017/07/25/shiro学习笔记-1/index.html">
<meta property="og:site_name" content="飞翔的美食号">
<meta property="og:description" content="一些废话在我写这篇博文时我大概断断续续学了三四天shiro,期间因为看的不认真的缘故 ，我对shiro一知半解根本算不上系统的学习。在我学习一样东西是我奉行的策略就是边学边做，于是我花了半天的时间模仿着别人写了一个例子，终于对shiro的权限控制流程有了个大致的了解。对于shiro张开涛老师讲的很细致.下面就开始我们的讲解。 shiro简介 Apache Shiroa是一个强大且易用的Java安全">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://otfz27j99.bkt.clouddn.com//shiro/d59f6d02-1f45-3285-8983-4ea5f18111d5.png">
<meta property="og:updated_time" content="2017-08-26T01:24:13.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="shiro学习笔记[1]">
<meta name="twitter:description" content="一些废话在我写这篇博文时我大概断断续续学了三四天shiro,期间因为看的不认真的缘故 ，我对shiro一知半解根本算不上系统的学习。在我学习一样东西是我奉行的策略就是边学边做，于是我花了半天的时间模仿着别人写了一个例子，终于对shiro的权限控制流程有了个大致的了解。对于shiro张开涛老师讲的很细致.下面就开始我们的讲解。 shiro简介 Apache Shiroa是一个强大且易用的Java安全">
<meta name="twitter:image" content="http://otfz27j99.bkt.clouddn.com//shiro/d59f6d02-1f45-3285-8983-4ea5f18111d5.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.flyfood.name/2017/07/25/shiro学习笔记-1/"/>





  <title>shiro学习笔记[1] | 飞翔的美食号</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">飞翔的美食号</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">The more effort ,the more lucky</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.flyfood.name/2017/07/25/shiro学习笔记-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="听孔子的话">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://otfz27j99.bkt.clouddn.com/bolg/imageQQ%E5%9B%BE%E7%89%8720170829174414.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="飞翔的美食号">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">shiro学习笔记[1]</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-25T21:43:36+08:00">
                2017-07-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/shiro教程/" itemprop="url" rel="index">
                    <span itemprop="name">shiro教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/2017/07/25/shiro学习笔记-1/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2017/07/25/shiro学习笔记-1/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="一些废话"><a href="#一些废话" class="headerlink" title="一些废话"></a>一些废话</h1><p>在我写这篇博文时我大概断断续续学了三四天shiro,期间因为看的不认真的缘故 ，我对shiro一知半解根本算不上系统的学习。在我学习一样东西是我奉行的策略就是边学边做，于是我花了半天的时间模仿着别人写了一个例子，终于对shiro的权限控制流程有了个大致的了解。对于shiro<a href="http://jinnianshilongnian.iteye.com/blog/2018398" target="_blank" rel="noopener">张开涛老师</a>讲的很细致.下面就开始我们的讲解。</p>
<h1 id="shiro"><a href="#shiro" class="headerlink" title="shiro"></a>shiro</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><blockquote>
<p>Apache Shiroa是一个强大且易用的Java安全框架,执行身份验证, 授权、密码学和会话管理。 通过Shiro易于理解的API,您可以 快速、轻松地安全的任何应用程序从最小的移动应用程序最大的网络 和企业应用程序。 </p>
<h2 id="shiro模块"><a href="#shiro模块" class="headerlink" title="shiro模块"></a>shiro模块</h2><p><img src="http://otfz27j99.bkt.clouddn.com//shiro/d59f6d02-1f45-3285-8983-4ea5f18111d5.png" alt="shiro的模块"><br>在讲解这个模型图之前咱们先说一下一个<strong>web应用</strong>用户登录的流程</p>
<ol>
<li>用户在登录页面输入自己的用户名和密码，然后点击登录。</li>
<li>服务器在在响应这个请求时开始对用户的信息进行比对，如果比对成功，那么进行下一步</li>
<li>我们开始给这个用户授权，就是给这个用户分配权限，那些事你可以做那些不可以做</li>
<li>用户登录成功开始做他能做的事<a id="more"></a>
一般来说shiro在第二步就开始发挥自己的作用</li>
</ol>
</blockquote>
<ul>
<li><strong>Authentication</strong>：这个英文单词的意思是<strong>身份验证</strong>我们在验证用户的身份时用的就是这个模块</li>
<li><strong>Authorization</strong>  ：这个是<strong>授权</strong>的意思给用户分配权限</li>
<li><strong>Session Manager</strong>：会话管理，即用户登录后就是一次会话，在没有退出之前，它的所有信息都在会话中；会话可以是普通JavaSE环境的，也可以是如Web环境的；</li>
<li><strong>Cryptography</strong>：加密，保护数据的安全性，如密码加密存储到数据库，而不是明文存储；</li>
<li><strong>Web Support</strong>：Web支持，可以非常容易的集成到Web环境；</li>
<li><strong>Caching</strong>：缓存，比如用户登录后，其用户信息、拥有的角色/权限不必每次去查，这样可以提高效率；</li>
<li><strong>Concurrency</strong>：shiro支持多线程应用的并发验证，即如在一个线程中开启另一个线程，能把权限自动传播过去；</li>
<li><strong>Testing</strong>：提供测试支持；</li>
<li><strong>Run As</strong>：允许一个用户假装为另一个用户（如果他们允许）的身份进行访问；</li>
<li><strong>Remember Me</strong>：记住我，这个是非常常见的功能，即一次登录后，下次再来的话不用登录了。</li>
</ul>
<p><strong>在此我们要记住一件事</strong> <em>shiro不会替我们维护用户的权限，它只提供了抽象的模块具体的实现需要我们自己去设计然后注入给shiro相应的接口就行了，他回去管理我们设计的安全策略</em></p>
<blockquote>
<p>一个优秀的框架一定是对外友好，对内易扩展的</p>
</blockquote>
<p>下面我们从外部看一下，如下图所示<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">st=&gt;start: Application code</span><br><span class="line">io=&gt;inputoutput: Subject</span><br><span class="line">op=&gt;operation: shiro SecurityManager</span><br><span class="line">e=&gt;end: Realm</span><br><span class="line"></span><br><span class="line">st-&gt;io-&gt;op-&gt;e</span><br></pre></td></tr></table></figure></p>
<p>Subject是直接跟应用程序交互的，在对外API中Subject是核心<br><strong>Subject</strong>：主体，代表了当前“用户”，这个用户不一定是一个具体的人，与当前应用交互的任何东西都是Subject，如网络爬虫，机器人等；即一个抽象概念；所有Subject都绑定到SecurityManager，与Subject的所有交互都会委托给SecurityManager；可以把Subject认为是一个门面；SecurityManager才是<strong>实际的执行者</strong>；</p>
<p><strong>SecurityManager</strong>：安全管理器；即所有与安全有关的操作都会与SecurityManager交互；且它管理着所有Subject；可以看出它是Shiro的核心，它负责与后边介绍的其他组件进行交互，如果学习过SpringMVC，你可以把它看成DispatcherServlet前端控制器；</p>
<p><strong>Realm</strong>：域，这个很重要也是一开始让我迷惑的地方它到底是用来干嘛的。上文我们也说了SecurityManager是shiro的核心统筹大局，<strong>SecurityManager</strong>主要是制定战略，真正的实行者是Realm。<strong>Realm</strong>负责战术，一般来说我们会自己实现Realm，然后在这里进行比对，验证用户是否有权操作，你也可以认为它是<em>security dataSource</em> 安全数据源</p>
<p>也就是说对于我们而言，最简单的一个Shiro应用：<br>1、应用代码通过Subject来进行认证和授权，而Subject又委托给SecurityManager；<br>2、我们需要给Shiro的SecurityManager注入Realm，从而让SecurityManager能得到合法的用户及其权限进行判断。</p>
<h1 id="后记"><a href="#后记" class="headerlink" title="后记"></a>后记</h1><p>在一开始学习时我们最应该先理解<em>Authentication-Authorization</em>这两个模块,shiro进行身份验证和授权的策略是什麽？它内部的流程是什麽？<br>另外在我们平常写项目时对用户进行身份比对时，一般与从数据库中抽取的数据进行比对，shiro抽象出一个模块<strong>Realm</strong>来做这件事，英文意思为<em>范围，域</em>的意思。shiro再进行身份验证与授权时，真正的执行者是<strong>Realm</strong>.<br>下一篇我们就细讲一下这个<strong>Realm</strong></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/shiro/" rel="tag"># shiro</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/07/23/建一个自己的博客站点3/" rel="next" title="建一个自己的博客站点(3)">
                <i class="fa fa-chevron-left"></i> 建一个自己的博客站点(3)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/16/黑客情人节/" rel="prev" title="黑客情人节">
                黑客情人节 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://otfz27j99.bkt.clouddn.com/bolg/imageQQ%E5%9B%BE%E7%89%8720170829174414.jpg"
               alt="听孔子的话" />
          <p class="site-author-name" itemprop="name">听孔子的话</p>
           
              <p class="site-description motion-element" itemprop="description">编程学习</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一些废话"><span class="nav-number">1.</span> <span class="nav-text"><a href="#&#x4E00;&#x4E9B;&#x5E9F;&#x8BDD;" class="headerlink" title="&#x4E00;&#x4E9B;&#x5E9F;&#x8BDD;"></a>&#x4E00;&#x4E9B;&#x5E9F;&#x8BDD;</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#shiro"><span class="nav-number">2.</span> <span class="nav-text"><a href="#shiro" class="headerlink" title="shiro"></a>shiro</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">2.1.</span> <span class="nav-text"><a href="#&#x7B80;&#x4ECB;" class="headerlink" title="&#x7B80;&#x4ECB;"></a>&#x7B80;&#x4ECB;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#shiro模块"><span class="nav-number">2.2.</span> <span class="nav-text"><a href="#shiro&#x6A21;&#x5757;" class="headerlink" title="shiro&#x6A21;&#x5757;"></a>shiro&#x6A21;&#x5757;</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#后记"><span class="nav-number">3.</span> <span class="nav-text"><a href="#&#x540E;&#x8BB0;" class="headerlink" title="&#x540E;&#x8BB0;"></a>&#x540E;&#x8BB0;</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span><a href="http://www.miitbeian.gov.cn">豫ICP备17036205号</a> </span>
  <span class="author" itemprop="copyrightHolder">听孔子的话</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cyteYNP3V';
      var conf = '783edd2cf1257e61fa2b5bf46ddb34d6';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  



  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  

  

  

</body>
</html>
